﻿using System;
using System.Windows.Input;

namespace Eswat.Client.Wpf.ViewModels
{
	public class DelegateCommand<T> : ICommand
	{
		private Predicate<T> _canExecute;

		private Action<T> _execute;

		private bool _lastCanExecute = false;

		public DelegateCommand(
			Predicate<T> canExecute,
			Action<T> execute)
		{
			this._canExecute = canExecute;
			this._execute = execute;
		}

		public event EventHandler CanExecuteChanged;

		public bool CanExecute(object parameter)
		{
			var param = (T)parameter;

			var canExecute = this._canExecute(param);

			return canExecute;
		}

		public void Execute(object parameter)
		{
			var param = (T)parameter;

			this._execute(param);
		}
	}
}
